package com.jushu.storbox.service;

import java.util.List;

import android.content.Context;



import com.jushu.storbox.database.DAOFactory;
import com.jushu.storbox.database.ITeamSpaceDao;
import com.jushu.storbox.database.ITeamSpaceMemberDao;
import com.jushu.storbox.entities.TeamSpaceInfo;
import com.jushu.storbox.entities.TeamSpaceMemberInfo;
import com.jushu.storbox.util.CollectionUtils;
import com.huawei.sharedrive.sdk.android.modelV2.response.TeamSpaceMemberShipsEntity;

public class TeamSpaceHelper {
	
	private static final String LOG_TAG="TeamSpaceHelper";
	
	/**
	 * 对比团队空间,从服务器获取团队空间列表和本地的对比，同时更新本地数据库。
	 * 
	 * @return
	 */
	public static boolean compareServiceClientTeamSpace(Context context,
			List<TeamSpaceMemberShipsEntity> serverList, List<TeamSpaceInfo> clientList) {
		boolean isChange = false;
		ITeamSpaceDao teamSpaceDao=DAOFactory.instance(context).getTeamSpaceDao();
		// 如果都为空
		if (CollectionUtils.isEmpty(serverList)
				&& CollectionUtils.isEmpty(clientList)) {
			isChange = false;
			return isChange;
		}
		// 如果服务端为空就删除本地
		if (CollectionUtils.isEmpty(serverList)) {
			for (TeamSpaceInfo teamSpaceInfo : clientList) {
				teamSpaceDao.deleteTeamSpace(teamSpaceInfo.getTeamSpaceId());
			}
			isChange = false;
			return isChange;
		}
		for (TeamSpaceMemberShipsEntity teamSpaceMemberShipsEntity : serverList) {
			boolean isUpdate = false;
			TeamSpaceInfo currentTeamSpace = null;
			for (TeamSpaceInfo teamSpaceInfo : clientList) {
				if (!teamSpaceMemberShipsEntity.getTeamspace().getId().equals(teamSpaceInfo.getTeamSpaceId())) {
					continue;
				}
				currentTeamSpace = teamSpaceInfo;
				if (teamSpaceMemberShipsEntity.getTeamspace().getCurNumbers() != teamSpaceInfo.getCurNumbers()) {
					isUpdate = true;
					break;
				}
				if (teamSpaceMemberShipsEntity.getTeamspace().getName() != teamSpaceInfo.getName()) {
					isUpdate = true;
					break;
				}
				if (!stringEqual(teamSpaceMemberShipsEntity.getTeamspace().getDescription(),teamSpaceInfo.getDescription())) {
					isUpdate = true;
					break;
				}
				if (teamSpaceMemberShipsEntity.getTeamspace().getSpaceUsed() != teamSpaceInfo.getSpaceUsed()) {
					isUpdate = true;
					break;
				}
				if (!stringEqual(teamSpaceMemberShipsEntity.getTeamspace().getOwnerBy(), teamSpaceInfo.getOwerBy())) {
					isUpdate = true;
					break;
				}
				if (!stringEqual(teamSpaceMemberShipsEntity.getTeamspace().getOwnedByUserName(),teamSpaceInfo.getOwnerByUserName())) {
					isUpdate = true;
					break;
				}
				if (teamSpaceMemberShipsEntity.getTeamRole() != teamSpaceInfo.getTeamRole()) {
					isUpdate = true;
					break;
				}
				
			}
			// 如果有更新就更新本地的文件夹。
			if (isUpdate) {
				teamSpaceDao.updateTeamSpace(teamSpaceMemberShipsEntity.getTeamspace());
			}
			// 如果没有这个服务端的文件夹
			if (currentTeamSpace == null) {
				isChange = true;
				teamSpaceDao.insertTeamSpace(teamSpaceMemberShipsEntity.getTeamspace(), teamSpaceMemberShipsEntity.getId(), teamSpaceMemberShipsEntity.getTeamRole());
			}
		}

		// 处理删除的
		for (TeamSpaceInfo teamSpaceInfo : clientList) {
			boolean isExist = false;
			for (TeamSpaceMemberShipsEntity teamSpaceMemberShipsEntity : serverList) {
				if (teamSpaceMemberShipsEntity.getTeamspace().getId().equals(teamSpaceInfo.getTeamSpaceId())) {
					isExist = true;
					break;
				}
			}
			if (!isExist) {
				isChange = true;
				teamSpaceDao.deleteTeamSpace(teamSpaceInfo.getTeamSpaceId());
			}
		}
		return isChange;
	}
	
	public static boolean stringEqual(String source, String target){
		if(source == null && target == null){
			return true;
		}else if(source != null && source.equals(target)){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 对比团队空间,从服务器获取团队空间列表和本地的对比，同时更新本地数据库。
	 * 
	 * @return
	 */
	public static boolean compareServiceClientTeamSpaceMember(Context context,
			List<TeamSpaceMemberShipsEntity> serverList, List<TeamSpaceMemberInfo> clientList) {
		boolean isChange = false;
		ITeamSpaceMemberDao teamSpaceMemberDao=DAOFactory.instance(context).getTeamSpaceMemberDao();
		// 如果都为空
		if (CollectionUtils.isEmpty(serverList)
				&& CollectionUtils.isEmpty(clientList)) {
			isChange = false;
			return isChange;
		}
		// 如果服务端为空就删除本地
		if (CollectionUtils.isEmpty(serverList)) {
			for (TeamSpaceMemberInfo teamSpaceMemberInfo : clientList) {
				teamSpaceMemberDao.deleteTeamSpaceMember(teamSpaceMemberInfo.getTeamSpaceId(),teamSpaceMemberInfo.getMemberShipsId());
			}
			isChange = false;
			return isChange;
		}
		for (TeamSpaceMemberShipsEntity teamSpaceMemberShipsEntity : serverList) {
			boolean isUpdate = false;
			TeamSpaceMemberInfo currentTeamSpace = null;
			for (TeamSpaceMemberInfo teamSpaceMemberInfo  : clientList) {
				if (!teamSpaceMemberShipsEntity.getTeamspace().getId().equals(teamSpaceMemberInfo.getTeamSpaceId())||!teamSpaceMemberShipsEntity.getId().equals(teamSpaceMemberInfo.getMemberShipsId())) {
					continue;
				}
				currentTeamSpace = teamSpaceMemberInfo;
				if (teamSpaceMemberShipsEntity.getMember().getId() != teamSpaceMemberInfo.getUserId()) {
					isUpdate = true;
					break;
				}
				
				if (teamSpaceMemberShipsEntity.getMember().getName() != teamSpaceMemberInfo.getName()) {
					isUpdate = true;
					break;
				}
				
				if (teamSpaceMemberShipsEntity.getTeamRole() != teamSpaceMemberInfo.getTeamRole()) {
					isUpdate = true;
					break;
				}
				
				if (teamSpaceMemberShipsEntity.getRole() != teamSpaceMemberInfo.getRole()) {
					isUpdate = true;
					break;
				}
				
			}
			// 如果有更新就更新本地的数据。
			if (isUpdate) {
				teamSpaceMemberDao.updataTeamSpaceMember(teamSpaceMemberShipsEntity);
			}
			// 如果没有这个服务端的数据
			if (currentTeamSpace == null) {
				isChange = true;
				teamSpaceMemberDao.insertTeamSpaceMember(teamSpaceMemberShipsEntity);
			}
		}

		// 处理删除的
		for (TeamSpaceMemberInfo teamSpaceMemberInfo : clientList) {
			boolean isExist = false;
			for (TeamSpaceMemberShipsEntity teamSpaceMemberShipsEntity : serverList) {
				if (teamSpaceMemberShipsEntity.getTeamspace().getId().equals(teamSpaceMemberInfo.getTeamSpaceId())&&teamSpaceMemberShipsEntity.getId().equals(teamSpaceMemberInfo.getMemberShipsId())) {
					isExist = true;
					break;
				}
			}
			if (!isExist) {
				isChange = true;
				teamSpaceMemberDao.deleteTeamSpaceMember(teamSpaceMemberInfo.getTeamSpaceId(),teamSpaceMemberInfo.getMemberShipsId());
			}
		}
		return isChange;
	}
	
	
}
